//
//  HomeYouLikeCell.swift
//  XiaoJingMallSwift
//
//  Created by beisheng03 on 2017/7/25.
//  Copyright © 2017年 beisheng. All rights reserved.
//

import UIKit

class HomeYouLikeCell: BaseTableViewCell {

    @IBOutlet weak var bgView: UIView!
    @IBOutlet weak var lineView: UIView!
    @IBOutlet weak var bottomConstr: NSLayoutConstraint!
    @IBOutlet weak var imageView1: UIImageView!
    @IBOutlet weak var imageView2: UIImageView!
    
    @IBOutlet weak var label1: UILabel!
    @IBOutlet weak var label2: UILabel!
    
    
    @IBOutlet weak var rightLabel1: UILabel!
    @IBOutlet weak var rightLabel2: UILabel!
    
    @IBOutlet weak var prieLabel1: UILabel!
    
    @IBOutlet weak var prieLabel2: UILabel!
    
    @IBOutlet weak var btn1: HomeLikeBtn!
    
    @IBOutlet weak var btn2: HomeLikeBtn!
    
    @IBOutlet weak var indicatorView1: UIActivityIndicatorView!
    @IBOutlet weak var indicatorView2: UIActivityIndicatorView!


    
    var likeItems : [HomeItemModel]?{
    
        didSet{
            
            let model1 = likeItems![0]
            
            indicatorView1.startAnimating()
            imageView1.sd_setImage(with: URL(string: model1.goods_image), placeholderImage: UIImage.defaulImage(), options: []) { (image, _, _, _) in
                self.indicatorView1.stopAnimating()
                //                self.imageView1.contentMode = .scaleAspectFit
                
            }
            

            
            
            label1.attributedText = attributedString2(str: model1.goods_name, font: UIFont.systemFont(ofSize: 13))
            
            //要匹配的字符串
            let tempStr =  "¥" + model1.goods_price
            
            
            prieLabel1.attributedText = attributedString(str: tempStr, font: UIFont.systemFont(ofSize: 13))
            
            if likeItems?.count == 2 {
                
                let model2 = likeItems![1]
                
                indicatorView2.startAnimating()
                
                imageView2.sd_setImage(with: URL(string: model2.goods_image), placeholderImage: UIImage.defaulImage(), options: []) { (image, _, _, _) in
                    self.indicatorView2.stopAnimating()
                    //                    self.imageView2.contentMode = .scaleToFill
                    
                }
                
                
                
                
                label2.attributedText = attributedString2(str: model2.goods_name, font: UIFont.systemFont(ofSize: 13))

                
                let tempStr2 =  "¥" + model2.goods_price

                prieLabel2.attributedText = attributedString(str: tempStr2, font: UIFont.systemFont(ofSize: 13))

                btn2.backgroundColor = UIColor.clear
                
                btn2.isUserInteractionEnabled = true

            }else{
            
                btn2.backgroundColor = UIColor.lineGray()
                
                btn2.isUserInteractionEnabled = false
                
            }
            
        
        }
    
    
    }
    
    
    func didTapLetfBtn(){
        
        let temp = GoodsDetailController()
        
        let model1 = likeItems![0]
        
        temp.urlStr = model1.detail_url
        
        viewController().navigationController?.pushViewController(temp, animated: true)

    }
    
    
    func didTapRightBtn(){
        
        if likeItems?.count == 2 {
            
            let temp = GoodsDetailController()
            
            let model2 = likeItems![1]
            
            temp.urlStr = model2.detail_url
            
            viewController().navigationController?.pushViewController(temp, animated: true)

        }
        
}
    
    
    func didTapLetfLabel(){
        
        let temp = GoodsListController()
        
        let model1 = likeItems![0]
        
        temp.goodId = model1.gc_id
        
        viewController().navigationController?.pushViewController(temp, animated: true)
    }
    
    
    func didTapRightLabel(){
        
        let temp = GoodsListController()
        
        let model2 = likeItems![1]

        temp.goodId = model2.gc_id
        
        viewController().navigationController?.pushViewController(temp, animated: true)
    }
    
    
    func attributedString(str: String, font: UIFont) -> NSAttributedString? {
        
        //编写规则
        let patten = "[0-9]{1,15}"
        //创建正则表达式对象
        
        //caseInsensitive  忽略大小写  最常用
        //dotMatchesLineSeparators 允许 . 匹配任意字符，包括回车换行
        let regex = try! NSRegularExpression(pattern: patten, options: NSRegularExpression.Options.caseInsensitive)
        
        //根据规则匹配指定字符串中第一个符合规则的字符串，并且返回一个NSTextCheckingResult
        let res = regex.firstMatch(in: str, options: NSRegularExpression.MatchingOptions.init(rawValue: 0), range:  NSRange(location: 0, length: str.characters.count))
        
        
        // 根据原有字符串生成属性字符串
        let strM = NSMutableAttributedString(string: str)
        
        strM.addAttribute(NSFontAttributeName, value: font, range: NSMakeRange(0, str.characters.count))
        
        strM.addAttribute(NSForegroundColorAttributeName, value: UIColor.red, range: NSMakeRange(0, str.characters.count))
        
        
        //        let paragr = NSMutableParagraphStyle.init()
        //
        //        paragr.firstLineHeadIndent = 6;
        //
        //        paragr.lineSpacing = 2;
        
        //        strM.addAttribute(NSParagraphStyleAttributeName, value: paragr, range: NSMakeRange(0, str.characters.count))
        
        if let temp = res?.range {
            
            strM.addAttribute(NSFontAttributeName, value: UIFont.systemFont(ofSize: 19), range:temp )
            
        }
        
        return strM
        
        
        
        
    }
    
    
    
    
    
    func attributedString2(str: String, font: UIFont) -> NSAttributedString? {
        
        
        let patten = "[\u{4e00}-\u{9fa5}]{1,}"
        //创建正则表达式对象
        
        //caseInsensitive  忽略大小写  最常用
        //dotMatchesLineSeparators 允许 . 匹配任意字符，包括回车换行
        let regex = try! NSRegularExpression(pattern: patten, options: NSRegularExpression.Options.caseInsensitive)
        
        //根据规则匹配指定字符串中第一个符合规则的字符串，并且返回一个NSTextCheckingResult
        let res1 = regex.matches(in: str, options: NSRegularExpression.MatchingOptions.init(rawValue: 0), range: NSRange(location: 0, length: str.characters.count))
        
        // 根据原有字符串生成属性字符串
        let strM = NSMutableAttributedString(string: str)
        
        strM.addAttribute(NSFontAttributeName, value: font, range:NSMakeRange(0, str.characters.count))
        
        strM.addAttribute(NSForegroundColorAttributeName, value: UIColor.red, range:NSMakeRange(0, str.characters.count) )
        
        for temp in res1{
            
            
            strM.addAttribute(NSForegroundColorAttributeName, value: UIColor.dakGray(), range:temp.range )
            
        }
        
        
        
        
        
        let paragr = NSMutableParagraphStyle.init()
        
        paragr.firstLineHeadIndent = 6;
        
        paragr.lineSpacing = 2;
        
        strM.addAttribute(NSParagraphStyleAttributeName, value: paragr, range: NSMakeRange(0, str.characters.count))
        
        
        
        return strM
        
        
        
        
        
        
    }
    
    
    
    
    
    
    
    
    override func awakeFromNib() {
        super.awakeFromNib()
        contentView.backgroundColor = UIColor.lineGray()
        lineView.backgroundColor = UIColor.lineGray()
        bottomConstr.constant = -5
        
        label1.textColor = UIColor.dakGray()
        label1.font = UIFont.systemFont(ofSize: 12)
        
        label2.textColor = UIColor.dakGray()
        label2.font = UIFont.systemFont(ofSize: 12)
        
        btn1.likeCell = self
        btn2.likeCell = self
        
        rightLabel1.addCornerAndBorder(borderColor: UIColor.lineGray(), borderWidth: 0.6, masksToBounds: true, cornerRadius: 5)
        rightLabel2.addCornerAndBorder(borderColor: UIColor.lineGray(), borderWidth: 0.6, masksToBounds: true, cornerRadius: 5)
        
        rightLabel1.backgroundColor = UIColor.white
        rightLabel2.backgroundColor = UIColor.white
        


    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }
    
}


class HomeLikeBtn: UIButton {
    
    weak var likeCell : HomeYouLikeCell!
    
//    override func awakeFromNib() {
//        super.awakeFromNib()
//        
//        
//        
//        let gest = UITapGestureRecognizer(target: self, action: #selector(test))
//        
//        gest.numberOfTouchesRequired = 1
//        
//    
//        gest.delegate = self
//        
//        QL1("??????")
//    }
//    
//    
//    func test() {
//        
//    }
    
    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        
        let touch = touches.first
        
        let point = touch?.location(in: likeCell.bgView)
        
        let rect1 = likeCell.rightLabel1.convert(likeCell.rightLabel1.bounds, to: likeCell.bgView)
        let rect2 = likeCell.rightLabel2.convert(likeCell.rightLabel2.bounds, to: likeCell.bgView)
        
        
        if point!.x > KScreenW * 0.5 {
            
            if rect2.contains(point!) == true {
                
                likeCell.didTapRightLabel()
                
            } else {
                
                likeCell.didTapRightBtn()

            }
            
        } else {
            
            
            if rect1.contains(point!) == true {
                
                likeCell.didTapLetfLabel()
                
            } else {
                
                likeCell.didTapLetfBtn()
                
            }

        }
        
    }
    
}

